package com.example.feidaolumanager

import android.app.Application
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStore
import com.example.feidaolumanager.repository.UserPreferencesRepository
import com.tencent.bugly.crashreport.CrashReport
import com.tencent.shiply.processor.DiffPkgHandler
import com.tencent.shiply.processor.OriginBasePkgFile
import com.tencent.upgrade.bean.UpgradeConfig
import com.tencent.upgrade.core.DefaultUpgradeStrategyRequestCallback
import com.tencent.upgrade.core.UpgradeManager


/**
 * Created with IntelliJ IDEA.
 * @Author: Liao Run
 * @Date: 2024-02-21 14:53
 * @Description:
 **/
private const val LAYOUT_PREFERENCE_NAME = "user_preferences"
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(
    name = LAYOUT_PREFERENCE_NAME
)

class MyApplication : Application() {
    lateinit var userPreferencesRepository: UserPreferencesRepository


    override fun onCreate() {
        super.onCreate()
        // 崩溃报告
        CrashReport.initCrashReport(applicationContext, "01db75548c", false)
        // 热更新
        val builder: UpgradeConfig.Builder = UpgradeConfig.Builder()
        val config = builder.appId("5333e866ac")
            .appKey("0fc13d22-a3bd-41ad-9d72-feb10d5b855d")
            .allow32BitApkOn64BitDevice(true) // 64位的系统允许下载32位apk
            .diffPkgHandler(DiffPkgHandler())  //初始化差量包Handler
            .basePkgFileForDiffUpgrade(OriginBasePkgFile())  // 表示基于原始文件生成差量包，不支持渠道包差量
            .build()
        UpgradeManager.getInstance().init(applicationContext, config)


        // 拉取升级任务
        UpgradeManager.getInstance()
            .checkUpgrade(false, null, DefaultUpgradeStrategyRequestCallback())

        userPreferencesRepository = UserPreferencesRepository(dataStore)

    }
}